package com.sptpc.alertdialogdemo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = findViewById(R.id.Btn_choose);
        btn2 = findViewById(R.id.Btn_list);
        btn3 = findViewById(R.id.Btn_Radio);
        btn4 = findViewById(R.id.Btn_diy);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog1();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog2();
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                diaglog3();
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog4();
            }
        });
    }

    private void diaglog3() {
        final String items[] = new String[]{"item1","item2","item3"};
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.mipmap.ic_launcher);builder.setTitle("itemsTitle");
        builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this,"你选择了" + items[i],Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d("JINX", "onClick: "+items[which+1]);
                dialog.dismiss();
                Toast.makeText(MainActivity.this,"你选择了" + items[which+1],Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("Cancel",null);
        builder.create().show();
    }
    private void dialog1() {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setIcon(R.mipmap.ic_launcher);
        alertDialog.setTitle("J：");
        alertDialog.setMessage("Just to do.");
        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Back", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
//                            dismissDialog(i);
            }
        });
        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        alertDialog.show();
    }
    private void dialog2(){
        final String items[] = new String[]{"item1","item2","item3"};
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.mipmap.ic_launcher);builder.setTitle("itemsTitle");
        builder.setPositiveButton("确认",null);
        builder.setNegativeButton("取消",null);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this,"你选择了【"+items[i]+"】",Toast.LENGTH_SHORT).show();
            }
        });
        builder.create().show();

    }
    private void dialog4() {
        MyDialog myDialog = new MyDialog.Builder(this)
                .setTitle("这是一个自定义弹窗")
                .setMessage("这是一个自定义内容")
                .create()
                ;
                myDialog.show();
    }
}
